#include "PAP_defs.h"
#include "RTC_HAL_QE.h"
#include <hidef.h>

// MODIFY THE FOLLOWING PREPROCESSOR DIRECTIVES TO CONFIGURE SYSTEM OPTIONS

    //#define WATCHDOG_ENABLED
    //#define WATCHDOG_TIMEOUT_SHORT  
    //#define STOP_MODE_ENABLED
    
    //#define RESET_OUTPUT_ENABLED 
    //#define BKGD_MODE_DISABLED
    //#define RESET_PIN_ENABLED
    
    
    //#define WATCHDOG_USE_BUS_CLOCK
    
    //#define SPI1_PIN_SELECT_ALTERNATE
    //#define ACOMP2_INPUT_CAPTURE 
    #define IIC1_PIN_SELECT_ALTERNATE
    //#define ACOMP1_INPUT_CAPTURE

// SYSTEM OPTION CONFIGURATION ENDS HERE

// CONFIGURE CLOCK GATING MECHANISM
// Comment out the modules not used by the application

    #define TPM3_CLOCK_GATING_DISABLED
    #define TPM2_CLOCK_GATING_DISABLED
    #define TPM1_CLOCK_GATING_DISABLED
    #define ADC_CLOCK_GATING_DISABLED
    //#define IIC2_CLOCK_GATING_DISABLED
    #define IIC1_CLOCK_GATING_DISABLED
    #define SCI2_CLOCK_GATING_DISABLED
    //#define SCI1_CLOCK_GATING_DISABLED
    
    #define DBG_CLOCK_GATING_DISABLED
    #define FLASH_CLOCK_GATING_DISABLED
    //#define IRQ_CLOCK_GATING_DISABLED
    //#define KBI_CLOCK_GATING_DISABLED
    //#define ACMP_CLOCK_GATING_DISABLED
    #define RTC_CLOCK_GATING_DISABLED
    //#define SPI2_CLOCK_GATING_DISABLED
    //#define SPI1_CLOCK_GATING_DISABLED

// CLOCK GATING CONFIGURATION ENDS HERE

// DO NOT MODIFY STARTS HERE (CLOCK GATING CONFIGURATION)

#ifdef TPM3_CLOCK_GATING_DISABLED
#define TPM3_CLOCK_GATING 0x80
#else
#define TPM3_CLOCK_GATING 0x00
#endif

#ifdef TPM2_CLOCK_GATING_DISABLED
#define TPM2_CLOCK_GATING 0x40
#else
#define TPM2_CLOCK_GATING 0x00
#endif

#ifdef TPM1_CLOCK_GATING_DISABLED
#define TPM1_CLOCK_GATING 0x20
#else
#define TPM1_CLOCK_GATING 0x00
#endif

#ifdef ADC_CLOCK_GATING_DISABLED
#define ADC_CLOCK_GATING 0x10
#else
#define ADC_CLOCK_GATING 0x00
#endif

#ifdef IIC2_CLOCK_GATING_DISABLED
#define IIC2_CLOCK_GATING 0x08
#else
#define IIC2_CLOCK_GATING 0x00
#endif

#ifdef IIC1_CLOCK_GATING_DISABLED
#define IIC1_CLOCK_GATING 0x04
#else
#define IIC1_CLOCK_GATING 0x00
#endif

#ifdef SCI2_CLOCK_GATING_DISABLED
#define SCI2_CLOCK_GATING 0x02
#else
#define SCI2_CLOCK_GATING 0x00
#endif

#ifdef SCI1_CLOCK_GATING_DISABLED
#define SCI1_CLOCK_GATING 0x01
#else
#define SCI1_CLOCK_GATING 0x00
#endif

#define SYSTEM_CLOCK_GATING_1 (TPM3_CLOCK_GATING | TPM2_CLOCK_GATING |      \
                                TPM1_CLOCK_GATING | ADC_CLOCK_GATING |      \
                                IIC1_CLOCK_GATING | IIC2_CLOCK_GATING |     \
                                SCI2_CLOCK_GATING | SCI1_CLOCK_GATING )



#ifdef DBG_CLOCK_GATING_DISABLED
#define DBG_CLOCK_GATING 0x80
#else
#define DBG_CLOCK_GATING 0x00
#endif

#ifdef FLASH_CLOCK_GATING_DISABLED
#define FLASH_CLOCK_GATING 0x40
#else
#define FLASH_CLOCK_GATING 0x00
#endif

#ifdef IRQ_CLOCK_GATING_DISABLED
#define IRQ_CLOCK_GATING 0x20
#else
#define IRQ_CLOCK_GATING 0x00
#endif

#ifdef KBI_CLOCK_GATING_DISABLED
#define KBI_CLOCK_GATING 0x10
#else
#define KBI_CLOCK_GATING 0x00
#endif

#ifdef ACMP_CLOCK_GATING_DISABLED
#define ACMP_CLOCK_GATING 0x08
#else
#define ACMP_CLOCK_GATING 0x00
#endif

#ifdef RTC_CLOCK_GATING_DISABLED
#define RTC_CLOCK_GATING 0x04
#else
#define RTC_CLOCK_GATING 0x00
#endif

#ifdef SPI2_CLOCK_GATING_DISABLED
#define SPI2_CLOCK_GATING 0x02
#else
#define SPI2_CLOCK_GATING 0x00
#endif

#ifdef SPI1_CLOCK_GATING_DISABLED
#define SPI1_CLOCK_GATING 0x01
#else
#define SPI1_CLOCK_GATING 0x00
#endif

#define SYSTEM_CLOCK_GATING_2 (DBG_CLOCK_GATING | FLASH_CLOCK_GATING |     \
                                IRQ_CLOCK_GATING | KBI_CLOCK_GATING |      \
                                ACMP_CLOCK_GATING | RTC_CLOCK_GATING |     \
                                SPI2_CLOCK_GATING | SPI1_CLOCK_GATING )


// DO NOT MODIFY - CLOCK GATING CONFIGURATION ENDS HERE

// DO NOT MODIFY - SOPT CONFIGURATION

#ifdef WATCHDOG_ENABLED
#define WATCHDOG_ENABLE 0x80
#else
#define WATCHDOG_ENABLE 0x00
#endif

#ifdef WATCHDOG_TIMEOUT_SHORT
#define WATCHDOG_TIMEOUT 0x00
#else
#define WATCHDOG_TIMEOUT 0x40
#endif

#ifdef STOP_MODE_ENABLED
#define STOP_MODE 0x20
#else
#define STOP_MODE 0x00
#endif

#ifdef BKGD_MODE_DISABLED
#define BKGD_MODE 0x00
#else
#define BKGD_MODE 0x02
#endif

#ifdef RESET_PIN_ENABLED
#define RESET_PIN 0x01
#else
#define RESET_PIN 0x00
#endif

#define SYS_CONFIG_1  (WATCHDOG_ENABLE | WATCHDOG_TIMEOUT | STOP_MODE |    \
                    BKGD_MODE | RESET_PIN)       

#ifdef WATCHDOG_USE_BUS_CLOCK
#define WATCHDOG_CLOCK 0x80
#else
#define WATCHDOG_CLOCK 0x00
#endif

#ifdef SPI1_PIN_SELECT_ALTERNATE
#define SPI1_PIN_SELECT 0x08
#else
#define SPI1_PIN_SELECT 0x00
#endif

#ifdef ACOMP2_INPUT_CAPTURE
#define ACOMP2_ICAPTURE 0x04
#else
#define ACOMP2_ICAPTURE 0x00
#endif
                    
#ifdef IIC1_PIN_SELECT_ALTERNATE
#define IIC1_PIN_SELECT 0x02
#else
#define IIC1_PIN_SELECT 0x00
#endif

#ifdef ACOMP1_INPUT_CAPTURE
#define ACOMP1_ICAPTURE 0x01
#else
#define ACOMP1_ICAPTURE 0x00
#endif

#define SYS_CONFIG_2 (WATCHDOG_CLOCK | SPI1_PIN_SELECT | ACOMP2_ICAPTURE |  \
                        IIC1_PIN_SELECT | ACOMP1_ICAPTURE )


// DO NOT MODIFY ENDS (SOPT CONFIG) HERE

#define Page_Erase   PGM[21] = 0x40; u8temp = (( uint8 (*)( uint16 ))( PGM ))
#define Program_Byte PGM[21] = 0x20; u8temp = (( uint8 (*)( uint16, uint8 ))( PGM ))

#define FLASH_ERROR 0x00
#define FLASH_OK 	0x01

#define SCHEDULER_TASK_MOTOR    0x08
#define SCHEDULER_TASK_SCITX    0x07
#define SCHEDULER_TASK_SCIRX    0x06
#define SCHEDULER_TASK_GPSIIC   0x05
#define SCHEDULER_TASK_SURFACE  0x04
#define SCHEDULER_TASK_SENSORS  0x03
#define SCHEDULER_TASK_MISC     0x02
#define SCHEDULER_TASK_HBLED    0x01

void vfnSystemOptionsConfig (void);

void vfnSystemClockGating (void);

void vfnConfigureScheduler (void);

void vfnPulseDelay (void);

void vfnFlash_Protect (void);

void vfnFlash_ConfigureClock (void);

uint8 u8FlashProgramByte (uint16 u16FlashPage, uint8 u8FlashByte);

uint8 u8FlashErase (uint16 u16FlashPage);